package cn.gson.crm.controller.counselor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.Jdbc;
import cn.gson.crm.model.dao.StudentDao;
import cn.gson.crm.model.dao.StudentPoDao;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.model.po.FollowStudentPo;

@WebServlet("/customer/detail")
public class CustomerDetail extends BaseServlet {

	private static final long serialVersionUID = 1L;
	private StudentPoDao stuPoDao = new StudentPoDao();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Employee emp = (Employee) request.getSession().getAttribute("emplo");
		/*String sql = "select m.*,(select count(1) from attach a where a.record = m.record_id"
				+ "  and a.model='follow_up') attach from follow_up m";
		List<Map<String, Object>> FollowList = Jdbc.getInstace().executeQueryForList(sql);*/
		
		
		String id = request.getParameter("id");
		String p = request.getParameter("page");
		if (p == null || p.isEmpty()) {
			p = "1";
		}
		Integer page = Integer.parseInt(p);
		// 获取学员信息
		FollowStudentPo po = stuPoDao.getStudentInfo(id);
		// 根据学员id查询学员记录信息
		List<FollowStudentPo> stuRecordList = stuPoDao.getFollowRecordById(page, id, emp.getId());
		Integer totalPage = stuPoDao.getRecordTotalPage(id, emp.getId());
		setReqAndRemoveRes("erro", request, "erro");
		// 获取系统当前时间
		request.setAttribute("now", sdf.format(new Date()));
		request.setAttribute("stuRecordList", stuRecordList);
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("page", page);
		request.setAttribute("queryString", noPageQueryString(request));
		request.setAttribute("stuDetail", po);
		request.setAttribute("emp", emp);
		//request.setAttribute("FollowList", FollowList);
		getServletPathAndForward(request, "counselor/cus_detail.jsp");
	}

	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Employee emp = (Employee) request.getSession().getAttribute("emplo");
		// 表单提交的跟进信息
		String[] attachs = request.getParameterValues("attachs");
		String stuId = request.getParameter("stuId");
		String content = request.getParameter("recordContent");
		String followTime = request.getParameter("followTime");
		String nextTime = request.getParameter("nextTime");

		FollowStudentPo stuPo = new FollowStudentPo();
		if (content == null || content.isEmpty()) {
			request.getSession().setAttribute("erro", "输入内容不能为空");
			get(request, response);
			return;
		} else if (content.trim() == null || content.trim().isEmpty()) {
			request.getSession().setAttribute("erro", "输入内容不能为空");
			get(request, response);
			return;
		}
		if (followTime == null || followTime.isEmpty()) {
			request.getSession().setAttribute("erro", "跟进时间不能为空");
			get(request, response);
			return;
		} else if (followTime.trim() == null || followTime.trim().isEmpty()) {
			request.getSession().setAttribute("erro", "跟进时间不能为空");
			get(request, response);
			return;
		}
		stuPo.setContent(content);
		stuPo.setCounselorId(emp.getId());
		stuPo.setId(Integer.parseInt(stuId));
		stuPo.setFollowTime(sdf.parse(followTime));
		if (nextTime != null && !nextTime.isEmpty()) {
			stuPo.setNextTime(sdf.parse(nextTime));
		} else {
			stuPo.setNextTime(null);
		}
		Integer recordId = stuPoDao.insertOneRecord(stuPo, emp.getId().toString());
		if(recordId != null){
			//有附件就关联附件信息
	 		//严格意义来说，这里是要做事务控制
			if(attachs!=null && attachs.length > 0){
				stuPoDao.connectAttach(recordId, attachs);
	 		}
			redirect("customer/detail?id="+stuId);
			return;
	 	}else{
	 		redirect("counselor/cus_detail.jsp");
	 	}
		get(request, response);
	}
}
